package com.core {

import com.Factory;
import com.interfaces.IView;
import com.utils.StringUtil;

import fairygui.GComponent;

import laya.utils.Handler;

public class View extends GComponent implements IView {

	/**
	 * 默认值表示不缓存此对象到映射表
	 */
	public static var NAME:String = "";
	/**
	 * 键值的缓存对象
	 */
	private static var object:Object = {};

	public function View() {
		super();
	}

	public function regAction(action:String, caller:*, method:Function, group:String=null):void {
		Factory.inst.regAction(action, caller, method, group);
	}

	public function regActionHandler(action:String, handler:Handler, group:String=null):void {
		Factory.inst.regActionHandler(action, handler, group);
	}

	public function removeAllAction(...arge):void {
		Factory.inst.removeAllAction.apply(Factory.inst, arge);
	}

	public function removeGroup(group:String):void {
		Factory.inst.removeGroup(group);
	}

	public function removeGroupActions(group:String, ...arge):void {
		arge.unshift(group);
		Factory.inst.removeGroupActions.apply(Factory.inst, arge);
	}

	public function removeHandler(action:String, method:Function, group:String = null):void {
		Factory.inst.removeHandler(action, method, group);
	}

	public function sendAction(action:String, ...arge):void {
		arge.unshift(action);
		Factory.inst.sendAction.apply(Factory.inst, arge);
	}

	public function sendGroupAction(group:String, action:String, ...arge):void {
		arge.unshift(action);
		arge.unshift(group);
		Factory.inst.sendGroupAction.apply(Factory.inst, arge);
	}

	public function addView(key:String, view:IView):Boolean {
		if (StringUtil.isNullOrEmpty(key)) {
			return false;
		}
		if (getView(key)) {
			console.warn("already exist key = "+key+", add failure!");
			return false;
		}
		object[key] = view;
		return true;
	}

	public function removeView(key:String):void {
		delete object[key];
	}

	public function getView(key:String):View {
		return object[key];
	}



}
}
